﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using Common;

namespace BLL.SchemaDiscovery
{
    [DataContract]
    public class FieldStats
    {
        [DataMember]
        public string FieldName { get; set; }
        [DataMember]
        public int SimpleDataType { get; set; }
        [DataMember]
        public int NonNullCount { get; set; }
        [DataMember]
        public int DistCount { get; set; }
        [DataMember]
        public string Max { get; set; }
        [DataMember]
        public string Min { get; set; }
        [DataMember]
        public string Mean { get; set; }
        [DataMember]
        public string Mode { get; set; }
        [DataMember]
        public string SD { get; set; }
        [DataMember]
        public Dictionary<string, int> Distributions { get; set; }
    }

    public class TableStatsManager
    {
        public static Dictionary<string, FieldStats> GetTableStats(
            string serverName, string dbName, string schemaName, string tblName)
        {
            string connStr = string.Format("Server={0};Database={1};Integrated Security=SSPI", serverName, dbName);
            TableColumn[] cols = DbUtil.GetTableColumns(
                            DataProviderType.MSSQL, connStr, schemaName, tblName);
            Dictionary<string, SqlDbType> colDbs = new Dictionary<string, SqlDbType>();
            foreach (TableColumn col in cols)
                colDbs.Add(col.ColumnName, col.DbType);
            int recCount = DbUtil.GetTableRecordCount(DataProviderType.MSSQL, connStr, schemaName, tblName);
            Dictionary<string, FieldStats> tblStats = new Dictionary<string, FieldStats>();
            foreach (string colName in colDbs.Keys)
            {
                try
                {
                    FieldStat fieldStat =
                        DbUtil.GetTableFieldStat(
                            DataProviderType.MSSQL, connStr, schemaName,
                            tblName, colName, colDbs[colName]);
                    FieldStats fieldStats = new FieldStats()
                                                {
                                                    DistCount=fieldStat.DistinctValueCount,
                                                    FieldName=fieldStat.FieldName,
                                                    Max=fieldStat.MaxValue==null?null:fieldStat.MaxValue.ToString(),
                                                    Min=fieldStat.MinValue==null?null:fieldStat.MinValue.ToString(),
                                                    Mean=fieldStat.Average==null?null:fieldStat.Average.ToString(),
                                                    Mode=fieldStat.Mode==null?null:fieldStat.Mode.ToString(),
                                                    NonNullCount=recCount-fieldStat.MissingValueCount,
                                                    SD=fieldStat.SD==null?null:fieldStat.SD.ToString(),
                                                    SimpleDataType=(int)fieldStat.DataType
                                                };
                    if(fieldStat.Distributions !=null && fieldStat.Distributions.Count>0)
                    {
                        fieldStats.Distributions=new Dictionary<string, int>();
                        foreach(object key in fieldStat.Distributions.Keys)
                        {
                            fieldStats.Distributions.Add(key.ToString(),fieldStat.Distributions[key]);
                        }
                    }
                    tblStats.Add(colName, fieldStats);
                }
                catch
                {
                }
            }
            return tblStats;
        }
    }
}
